package sunstar.com.xq.fragment

import org.jetbrains.anko.startActivity
import android.support.v4.widget.NestedScrollView
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.text.style.BulletSpan
import android.view.View
import android.widget.AdapterView
import android.widget.ImageView
import com.squareup.picasso.Picasso
import com.stx.xhb.xbanner.XBanner
import com.umeng.socialize.utils.Log.toast
import kotlinx.android.synthetic.main.main_base_fragment.*
import me.samthompson.bubbleactions.BubbleActions
import me.samthompson.bubbleactions.Callback
import org.jetbrains.anko.AnkoLogger
import sunstar.com.xq.R
import sunstar.com.xq.activity.MultiImageInfoActivity
import sunstar.com.xq.activity.SingleImageInfoActivity
import sunstar.com.xq.activity.VideoInfoActivity
import sunstar.com.xq.adapter.SquareListAdapter
import sunstar.com.xq.databean.BannerData
import sunstar.com.xq.databean.CardData
import sunstar.com.xq.presenter.SquarePresenter
import sunstar.com.xq.util.DialogUtil
import sunstar.com.xq.view.SquareView

/**
 * Created by Sun.Star on 2017/6/8.
 */
class SquareFragment : LazyFragment(),SquareView,AnkoLogger{
    var listener : NestedScrollView.OnScrollChangeListener?= null
    private var adapter: SquareListAdapter?=null
    var dialogUtil : DialogUtil?=null
    var dataList = mutableListOf<CardData>()


    var presenter = SquarePresenter(this)


    private var scrollView : NestedScrollView? = null
    private var mBanner : XBanner? = null
    private var squareDataView:RecyclerView? = null
    fun setScrollListener(listener:NestedScrollView.OnScrollChangeListener){
        this.listener = listener
    }
    override fun initViewid(): Int  = R.layout.main_base_fragment
    override fun initViewCompelte() {
        dialogUtil = DialogUtil(activity)
        dialogUtil!!.show()
        mBanner = thisView.findViewById(R.id.main_banner) as XBanner
        scrollView = thisView.findViewById(R.id.main_base_fragment_scollview) as NestedScrollView
        squareDataView = thisView.findViewById(R.id.square_recycle) as RecyclerView
        scrollView!!.setOnScrollChangeListener { v: NestedScrollView?, scrollX: Int, scrollY: Int, oldScrollX: Int, oldScrollY: Int ->
            if(listener!=null){
                listener!!.onScrollChange(v,scrollX,scrollY,oldScrollX,oldScrollY)
            }
        }
        mBanner!!.setmAdapter { banner, model, view, position ->
            Picasso.with(activity).load(presenter.imgList[position]).into(view as ImageView)
        }
        adapter = SquareListAdapter(dataList,activity)
        squareDataView!!.adapter = adapter
        var mManager = LinearLayoutManager(activity)
        mManager.setSmoothScrollbarEnabled(true)
        mManager.isAutoMeasureEnabled = true
        squareDataView!!.layoutManager = mManager
        squareDataView!!.setHasFixedSize(true)
        squareDataView!!.isNestedScrollingEnabled = false
        adapter!!.itemClickListener =AdapterView.OnItemClickListener { parent, view, position, id ->
            if(adapter!!.getItemViewType(position)==adapter!!.singleImgType){
                activity.startActivity<SingleImageInfoActivity>("data" to dataList[position])
            }else if(adapter!!.getItemViewType(position)==adapter!!.video){
                activity.startActivity<VideoInfoActivity>("data" to dataList[position])
            }else if(adapter!!.getItemViewType(position)==adapter!!.imgsType){
                activity.startActivity<MultiImageInfoActivity>("data" to dataList[position])
            }

        }
        var bubbleAction:BubbleActions
        adapter!!.itemLongClickListener = AdapterView.OnItemLongClickListener { parent, view, position, id ->
            BubbleActions.on(view).addAction("收藏",R.mipmap.cang,object : Callback{
                override fun doAction() {
                    toast(activity,"收藏")
                }
            }).addAction("分享",R.mipmap.xiang,object : Callback{
                override fun doAction() {
                    toast(activity,"分享")
                }
            }).show()
            false
        }
    }
    override fun initData() {

        presenter.initData(dataList)
    }
    override fun initBannerData(imgData: MutableList<String>,textData: MutableList<String>) {
        checkNet()
        main_banner.setData(imgData,textData)
    }

    override fun adapterNotify() {
        checkNet()
        adapter!!.notifyDataSetChanged()
    }

    override fun initDataView(dataList: MutableList<CardData>) {

    }


    private var netNumber = 0
    fun checkNet(){
        netNumber+=1
        if(netNumber == 2){
            dialogUtil!!.dismiss()
        }
    }
}